<template>
  <div ref="canvas"></div>
</template>

<script lang="ts" setup>
import * as THREE from "three";
import Stats from "three/examples/jsm/libs/stats.module";
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import {ref, onMounted, nextTick} from "vue";
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
import { MTLLoader } from 'three/addons/loaders/MTLLoader.js';

const canvas = ref();

const scene = new THREE.Scene();

const renderer = new THREE.WebGLRenderer({ antialias : true, });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0x000000);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight);
camera.position.set(50, 50, 50);
camera.lookAt(0, 15, 0);

const orbitControl = new OrbitControls(camera, renderer.domElement);
orbitControl.update();

const stat = Stats();

const keyLight = new THREE.SpotLight(0xffffff);
keyLight.position.set(0, 80, 80);
keyLight.intensity = 2;
keyLight.lookAt(new THREE.Vector3(0, 15, 0));
keyLight.castShadow = true;
keyLight.shadow.mapSize.height = 4096;
keyLight.shadow.mapSize.width = 4096;
scene.add(keyLight);

const backlight1 = new THREE.SpotLight(0xaaaaaa);
backlight1.position.set(150, 40, -20);
backlight1.intensity = 0.5;
backlight1.lookAt(new THREE.Vector3(0, 15, 0));
scene.add(backlight1);

const backlight2 = new THREE.SpotLight(0xaaaaaa);
backlight2.position.set(-150, 40, -20);
backlight2.intensity = 0.5;
backlight2.lookAt(new THREE.Vector3(0, 15, 0));
scene.add(backlight2);

const loader = new MTLLoader(undefined);
loader.load(
    new URL("@/assets/models/pinecone/pinecone.mtl", import.meta.url).href,
    (materials) => {
      console.log(' load mtl materials ', materials);
      materials.preload();

      new OBJLoader(undefined)
          .setMaterials(materials)
          .load(
              new URL("@/assets/models/pinecone/pinecone.obj", import.meta.url).href,
              (object) => {
                // object.children.forEach(function (child) {
                //   child.geometry.computeVertexNormals();
                //   child.geometry.computeFaceNormals();
                // });
                object.scale.set(120,120,120);
                scene.add(object);
                console.log(' OBJLoader load object: ', object);
              },
              (e) => {
                console.log(' loader.load onProgress ', e, '\r\n ', ( e.loaded / e.total * 100 ) + '% loaded');
              },
              (err) => {
                console.error(' loader.load onError ', err);
              }
          );
    }
);

const render = () => {
  stat.update();

  requestAnimationFrame(render);
  renderer.render(scene, camera);
};

onMounted(() => {
  nextTick(() => {
    canvas.value.appendChild(stat.domElement);
    canvas.value.appendChild(renderer.domElement);
    render();
  });
});
</script>
